package com.kll.tv.views

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kll.carolinehmibiz.AccountManager
import com.kll.carolinehmibiz.net.bean.LoginResponse
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.tv.base.views.BaseActivity
import com.kll.tv.databinding.ActivitySwitchAccountBinding
import com.kll.carolinehmibiz.base.utils.L
import com.kll.tv.views.adapter.SwitchAccountAdapter

/**
 * 切换账号页
 */
class SwitchAccountActivity: BaseActivity<BasePresenter>(), SwitchAccountAdapter.OnAccountListItemClickListener {
    private lateinit var binding: ActivitySwitchAccountBinding
    private var adapter: SwitchAccountAdapter? = null

    override fun initViewBinding(): View {
        binding = ActivitySwitchAccountBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createPresenter(): BasePresenter {
        return BasePresenter(this)
    }

    override fun init() {
        // 初始化adapter
        if (adapter == null) {
            val accountListManager = LinearLayoutManager(this)
            accountListManager.orientation = LinearLayoutManager.VERTICAL
            binding.rvContent.layoutManager = accountListManager
            adapter = SwitchAccountAdapter(this)
            binding.rvContent.adapter = adapter
            adapter!!.setOnAccountListItemClickListener(this)
        }

        initData()
        initListener()
    }

    private fun initData() {
        adapter!!.data = AccountManager.getInstancce().getAllAccount()
    }

    private fun initListener() {
        binding.tvCancel.setOnClickListener {
            finish()
        }
        // 添加账号
        binding.tvAddAccount.setOnClickListener {
            startActivity(Intent(this, LoginActivity::class.java))
        }
    }

    /**
     * item点击回调
     */
    @SuppressLint("NotifyDataSetChanged")
    override fun onAccountItemClick(data: LoginResponse) {
        L.d("TAG", "onAccountItemClick: ---------------------->")
        AccountManager.getInstancce().switchLoginAccount(data.user!!.mobile)
        adapter!!.notifyDataSetChanged()
    }
}